import Taro from '@tarojs/taro';
import u from 'updeep'
import request from '../util/request'

const INITDATA = {
  nameLike: '', // 搜索
  urlData: '', // 调用接口
  loadUrl: '', // 调用接口
  navName: '', // 顶部名称
  listData: [], // 列表数据
  districtType: {
    id: 0,
    name: '不限'
  },
  districtTypeRange: [],
  limit: 8,
  offset: 0,
  total: 0, // 总数
  type: 0, //0:求职，1：招聘
  typeList: [
    {
      value: 0,
      label: '求职'
    }, {
      value: 1,
      label: '招聘'
    }
  ]
}
export default {
  namespace: 'businessList',
  state: {...INITDATA},
  reducers: {
    updateState(state, {payload}) {
      return u({
        ...payload
      }, state)
    },
    initData(state, action) {
      return {
        ...state,
        ...action.payload,
        ...INITDATA
      }
    }
  },
  effects: {
    // 获取列表数据
    * loadProductData({payload = {}}, {select, put, call}) {
      const {
        limit, listData, type, districtType, nameLike
      } = yield select(({businessList}) => businessList)
      const name = payload.nameLike || nameLike;

      const param = {
        limit, type,
        nameLike: name,
        offset: listData.length, districtId: districtType.id
      }
      let url = 'service/product/getPage';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })

      if (data && data.rows instanceof Array) {

        if (data.rows.length == 0) {
          Taro.showToast({
            title: (data || {}).msg || '没有更多数据',
            icon: 'none'
          })
        } else {
          const d = data.rows || {};
          yield put({
            type: 'updateState',
            payload: {
              listData: listData.concat(d),
              total: data.total
            }
          })
        }

      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    // 获取列表数据
    * loadDeviceData({payload = {}}, {select, put, call}) {
      const {
        limit, listData, type, districtType, nameLike
      } = yield select(({businessList}) => businessList)
      const param = {
        limit, type,
        offset: listData.length, districtId: districtType.id
      }
      let url = 'service/device/getPage';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })
      if (data && data.rows instanceof Array) {

        if (data.rows.length == 0) {
          Taro.showToast({
            title: (data || {}).msg || '没有更多数据',
            icon: 'none'
          })
        } else {
          const d = data.rows || {};
          yield put({
            type: 'updateState',
            payload: {
              listData: listData.concat(d),
              total: data.total
            }
          })
        }

      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    // 获取列表数据
    * loadOrderData({payload = {}}, {select, put, call}) {
      const {
        limit, listData, type, districtType
      } = yield select(({businessList}) => businessList)
      const param = {
        limit, type,
        offset: listData.length, districtId: districtType.id
      }
      let url = 'service/order/getPage';

      const {data} = yield call(request, {
        url: SERVERURL + url,
        data: param,
        method: 'post',
      })
      if (data && data.rows instanceof Array) {

        if (data.rows.length == 0) {
          Taro.showToast({
            title: (data || {}).msg || '没有更多数据',
            icon: 'none'
          })
        } else {
          const d = data.rows || {};
          yield put({
            type: 'updateState',
            payload: {
              listData: listData.concat(d),
              total: data.total
            }
          })
        }

      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    },
    * loadDistrict({payload = {}}, {put, call}) {
      const {data} = yield call(request, {
        url: SERVERURL + '/service/city/all',
        method: 'get',
      })
      if (data && data.data instanceof Array) {
        const d = data.data || {};
        yield put({
          type: 'updateState',
          payload: {
            districtTypeRange: d
          }
        })
      } else {
        Taro.showToast({
          title: (data || {}).msg || '获取列表数据失败',
          icon: 'none'
        })
      }
    }
  }
};
